<?php
namespace app\common\model\user;

use think\Model;

class LabelReward extends  Model{

    protected $name = 'user_label_reward';

    public function getList($label_id=0){
        $list=$this->with(['image'])->where(['label_id'=>$label_id])->order(['sort' => 'desc'])->select();
        return $list;
    }
    public function image()
    {
        return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_id');
    }

    public function label()
    {
        return $this->hasOne('app\store\model\user\UserLabel', 'label_id', 'label_id');
    }

    public function getApiList(){
        $list=$this->with(['image','label'])->order(['sort' => 'desc'])->select();

        return $list;
    }
    public function getApiLabelId($reward_id){
        $label_id=$this->where(['reward_id'=>$reward_id])->value('label_id');
        return $label_id;
    }

    /**
     * 新增记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        if (empty($data['image_id'])) {
            $this->error = '请上传图标';
            return false;
        }
        $data['label_id']=intval($data['label_id']);
        return $this->allowField(true)->save($data);
    }

    /**
     * 更新记录
     * @param $data
     * @return bool|int
     */
    public function edit($data)
    {
        if (empty($data['image_id'])) {
            $this->error = '请上传图标';
            return false;
        }

        return $this->allowField(true)->save($data) !== false;
    }
    /**
     * 轮播图详情
     * @param $id
     * @return null|static
     * @throws \think\exception\DbException
     */
    public static function detail($id)
    {
        return self::get($id, ['image']);
    }
}